﻿using System;
using SBT;
using System.DHTML;
using System.Silverlight;
using System.Silverlight.Presentation;

namespace Scripts03
{
    public class LayoutMgr : SLManager
    {
        private bool m_initialized = false;
        private bool m_downloaded = false;
        private Button m_btnCancel;
        private AgBehavior m_mobile;
        private ToggleButtonList m_horizontalList;
        private ToggleButtonList m_verticalList;
        private StackPanel m_stackPanel;
        private FlowPanel m_tagCloud;
        Downloader m_downloader;
        public LayoutMgr(DOMElement host)
            : base(host)
        {
        }

        public override void OnSilverlightLoaded(System.Silverlight.SilverlightControl slControl, object args)
        {
            ControlContent content = slControl.Content;
            m_downloader = (Downloader)slControl.CreateObject("downloader");
            Sys.EventHandler downloadHandler = OnDownloaderComplete;

            m_downloader.AddEventListener(DownloaderEvent.Completed, downloadHandler);
            m_downloader.Open("GET", "Templates.zip");
            m_downloader.Send();
            // Begin behavior creation
            CreateLayoutedButtons(slControl, content);
            CreateGridAndMobile(slControl, content);
            m_stackPanel = new StackPanel();
            m_stackPanel.Plugin = slControl;
            m_stackPanel.Element = (UIElement) content.FindName("StackPanel");
            m_stackPanel.Initialize();
            m_tagCloud = new FlowPanel();
            m_tagCloud.Plugin = slControl;
            m_tagCloud.Element = (UIElement)content.FindName("TagCloud");
            m_tagCloud.Initialize();
            RandomTagSizes();
            // End behavior creation
            m_initialized = true;
            if (m_downloaded)
                ApplyAnimTemplates();
        }

        private void CreateGridAndMobile(SilverlightControl plugin, ControlContent pluginContent)
        {
            this.m_mobile = new AgBehavior();
            m_mobile.Plugin = plugin;
            m_mobile.Element = (UIElement)pluginContent.FindName("mobile");
            m_mobile.SetNamedValue(Grid.HorizontalAlignment, HorizontalAlignment.Center);
            m_mobile.SetNamedValue(Grid.VerticalAlignment, VerticalAlignment.Center);
            m_mobile.SetNamedValue(Grid.MarginBottom, 4);
            m_mobile.SetNamedValue(Grid.MarginLeft, 4);
            m_mobile.SetNamedValue(Grid.MarginRight, 4);
            m_mobile.SetNamedValue(Grid.MarginTop, 4);

            m_mobile.Initialize();
            Grid grid = new Grid();
            grid.Plugin = plugin;
            grid.Element = (UIElement)pluginContent.FindName("gridSample");
            grid.Initialize();

            ToggleButton horLeft = new ToggleButton();
            horLeft.Plugin = plugin;
            horLeft.Element = (UIElement)pluginContent.FindName("horLeft");
            horLeft.Initialize();

            ToggleButton horCenter = new ToggleButton();
            horCenter.Plugin = plugin;
            horCenter.Element = (UIElement)pluginContent.FindName("horCenter");
            horCenter.Initialize();

            ToggleButton horRight = new ToggleButton();
            horRight.Plugin = plugin;
            horRight.Element = (UIElement)pluginContent.FindName("horRight");
            horRight.Initialize();

            Sys.EventHandler horHandler = OnHorizontalAlignmentChanged;
            this.m_horizontalList = new ToggleButtonList();
            m_horizontalList.Plugin = plugin;
            m_horizontalList.Items = (ArrayList)(object)new ToggleButton[] { horLeft, horCenter, horRight };
            m_horizontalList.Behavior = ToggleButtonListBehavior.RadioButtonList;
            m_horizontalList.add_selectionChanged(horHandler);
            m_horizontalList.Initialize();

            horCenter.IsPushed = true;

            ToggleButton vertTop = new ToggleButton();
            vertTop.Plugin = plugin;
            vertTop.Element = (UIElement)pluginContent.FindName("vertTop");
            vertTop.Initialize();

            ToggleButton vertCenter = new ToggleButton();
            vertCenter.Plugin = plugin;
            vertCenter.Element = (UIElement)pluginContent.FindName("vertCenter");
            vertCenter.Initialize();

            ToggleButton vertBottom = new ToggleButton();
            vertBottom.Plugin = plugin;
            vertBottom.Element = (UIElement)pluginContent.FindName("vertBottom");
            vertBottom.Initialize();

            Sys.EventHandler vertHandler = OnVerticalAlignmentChanged;
            m_verticalList = new ToggleButtonList();
            m_verticalList.Plugin = plugin;
            m_verticalList.Items = (ArrayList)(object)new ToggleButton[] { vertTop, vertCenter, vertBottom };
            m_verticalList.Behavior = ToggleButtonListBehavior.RadioButtonList;
            m_verticalList.add_selectionChanged(vertHandler);
            m_verticalList.Initialize();
            vertCenter.IsPushed = true;
        }
        private void OnHorizontalAlignmentChanged(object sender, Sys.EventArgs args)
        {
            ArrayList selectedItems = ((ToggleButtonList) sender).SelectedItems;
            if (selectedItems.Length == 1)
            {
                switch (((ToggleButton) selectedItems[0]).ElementName)
                {
                    case "horLeft":
                        this.m_mobile.SetNamedValue(Grid.HorizontalAlignment, HorizontalAlignment.Left);
                        break;
                    case "horCenter":
                        this.m_mobile.SetNamedValue(Grid.HorizontalAlignment, HorizontalAlignment.Center);
                        break;
                    case "horRight":
                        this.m_mobile.SetNamedValue(Grid.HorizontalAlignment, HorizontalAlignment.Right);
                        break;
                }
            }
        }
        private void OnVerticalAlignmentChanged(object sender, Sys.EventArgs args)
        {
            ArrayList selectedItems = ((ToggleButtonList)sender).SelectedItems;
            if (selectedItems.Length == 1)
            {
                switch (((ToggleButton)selectedItems[0]).ElementName)
                {
                    case "vertTop":
                        this.m_mobile.SetNamedValue(Grid.VerticalAlignment, VerticalAlignment.Top);
                        break;
                    case "vertCenter":
                        this.m_mobile.SetNamedValue(Grid.VerticalAlignment, VerticalAlignment.Center);
                        break;
                    case "vertBottom":
                        this.m_mobile.SetNamedValue(Grid.VerticalAlignment, VerticalAlignment.Bottom);
                        break;
                }
            }
        }

        private void CreateLayoutedButtons (SilverlightControl plugin,ControlContent pluginContent)
        {
            UIElement buttonOkCanvas = (UIElement) pluginContent.FindName("buttonOk");
            UIElement buttonCancelCanvas = (UIElement)pluginContent.FindName("buttonCancel");
            Button btnOk = new Button();
            btnOk.Plugin = plugin;
            btnOk.Element = buttonOkCanvas;
            btnOk.Initialize();

            m_btnCancel = new Button();
            m_btnCancel.Plugin = plugin;
            m_btnCancel.Element = buttonCancelCanvas;
            m_btnCancel.Initialize();

            AgBehavior btnOkText = new AgBehavior();
            btnOkText.Plugin = plugin;
            btnOkText.Element = (UIElement)pluginContent.FindName("buttonOk_text");
            btnOkText.SetNamedValue(Grid.HorizontalAlignment, HorizontalAlignment.Center);
            btnOkText.SetNamedValue(Grid.VerticalAlignment, VerticalAlignment.Center);
            btnOkText.Initialize();

            AgBehavior btnCancelText = new AgBehavior();
            btnCancelText.Plugin = plugin;
            btnCancelText.Element = (UIElement)pluginContent.FindName("buttonCancel_text");
            btnCancelText.SetNamedValue(Grid.HorizontalAlignment, HorizontalAlignment.Center);
            btnCancelText.SetNamedValue(Grid.VerticalAlignment, VerticalAlignment.Center);
            btnCancelText.Initialize();

            Grid gridOk = new Grid();
            gridOk.Plugin = plugin;
            gridOk.Element = buttonOkCanvas;
            gridOk.Initialize();

            Grid gridCancel = new Grid();
            gridCancel.Plugin = plugin;
            gridCancel.Element = buttonCancelCanvas;
            gridCancel.Initialize();
            
        }

        private void OnDownloaderComplete(object sender, Sys.EventArgs args)
        {
            m_downloaded = true;
            if (m_initialized)
                ApplyAnimTemplates();
        }

        private void ApplyAnimTemplates()
        {
            string down = this.m_downloader.GetResponseText("AlignmentButtonDown.xaml");
            string up = this.m_downloader.GetResponseText("AlignmentButtonUp.xaml");
            string enter = this.m_downloader.GetResponseText("AlignmentButtonEnter.xaml");
            string leave = this.m_downloader.GetResponseText("AlignmentButtonLeave.xaml");

            ArrayList items = m_horizontalList.Items;
            for (int i = 0; i < items.Length; i++)
            {
                ToggleButton btn = (ToggleButton)items[i];
                btn.MouseDownTemplate = down;
                btn.MouseEnterTemplate = enter;
                btn.MouseLeaveTemplate = leave;
                btn.MouseUpTemplate = up;
            }

            items = m_verticalList.Items;
            for (int i = 0; i < items.Length; i++)
            {
                ToggleButton btn = (ToggleButton)items[i];
                btn.MouseDownTemplate = down;
                btn.MouseEnterTemplate = enter;
                btn.MouseLeaveTemplate = leave;
                btn.MouseUpTemplate = up;
            }

            ((UIElement)Plugin.Content.FindName("loadingPanel")).Visibility = Visibility.Collapsed;
        }

        public void ChangeCancelBtnSize(double newSize)
        {
            m_btnCancel.Height = newSize;
        }
        public void ChangeStackPanelSize(double newSize)
        {
            m_stackPanel.Width = newSize;
        }
        public void RandomTagSizes()
        {
            UIElementCollection tags = ((Canvas)m_tagCloud.Element).Children;
            int count = tags.Count;
            for (int i = 0; i < count; i++)
            {
                TextBlock tag = (TextBlock) tags.GetItem(i);
                tag.FontSize = (Math.Random() * 10 % 15) + 6;
            }
            m_tagCloud.InvalidateLayout();
        }
        /*
    randomTagSizes : function()
    {
        var count = this._tagCloudBehavior.get_element().Children.Count;
        var tags = this._tagCloudBehavior.get_element().Children;
        for(var i=0; i<count;i++)
        {
            var tag = tags.getItem(i);
            tag.fontSize = (Math.random()*10 %15)+6;
        }
        this._tagCloudBehavior.invalidateLayout();
    }*/
    }
}